//
//  RestaurantImageExtension.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "RestaurantImageExtension.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"


@implementation RestaurantImage(Extension)

+ (BOOL) initRestaurantImage{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return NO;
	}
	NSMutableArray *resImgArray = [[[NSMutableArray alloc]init] autorelease];
	NSMutableDictionary *resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:1] forKey:@"id"];
	[resImgDict setObject: @"cha_ca_mientrung.jpg" forKey:@"data"];
	[resImgDict setObject:@"Bún chả cá miền trung" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:20] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:2] forKey:@"id"];
	[resImgDict setObject: @"bun_sua_bd.jpg" forKey:@"data"];
	[resImgDict setObject:@"Bún sứa Bình Định" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:10] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:3] forKey:@"id"];
	[resImgDict setObject: @"bot.jpg" forKey:@"data"];
	[resImgDict setObject:@"Quán Bột" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:8] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:4] forKey:@"id"];
	[resImgDict setObject: @"che_thap_cam_hn.jpg" forKey:@"data"];
	[resImgDict setObject:@"Chè thập cẩm Hà Nội" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:5] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:5] forKey:@"id"];
	[resImgDict setObject: @"hai_tu_quy.jpg" forKey:@"data"];
	[resImgDict setObject:@"Hải Tứ Quý" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:15] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:6] forKey:@"id"];
	[resImgDict setObject: @"hutieu_thap_cam.jpg" forKey:@"data"];
	[resImgDict setObject:@"Hủ tiếu thập cẩm" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:19] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:6] forKey:@"id"];
	[resImgDict setObject: @"nem_ninh_hoa.jpg" forKey:@"data"];
	[resImgDict setObject:@"Nem Ninh Hòa" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:12] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:7] forKey:@"id"];
	[resImgDict setObject: @"oc_xuan.jpg" forKey:@"data"];
	[resImgDict setObject:@"Quán Ốc Xuân" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:13] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:8] forKey:@"id"];
	[resImgDict setObject: @"nem_ninh_hoa.jpg" forKey:@"data"];
	[resImgDict setObject:@"Nem Ninh Hòa" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:12] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:9] forKey:@"id"];
	[resImgDict setObject: @"OcLa.jpg" forKey:@"data"];
	[resImgDict setObject:@"Ốc Xinh" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:1] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:10] forKey:@"id"];
	[resImgDict setObject: @"soc_trang.jpg" forKey:@"data"];
	[resImgDict setObject:@"Bún nước lèo Sóc Trăn" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:9] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:11] forKey:@"id"];
	[resImgDict setObject: @"sushi.jpg" forKey:@"data"];
	[resImgDict setObject:@"sushi" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:11] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:12] forKey:@"id"];
	[resImgDict setObject: @"trang_bang.jpg" forKey:@"data"];
	[resImgDict setObject:@"Trảng Bàng" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:6] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:13] forKey:@"id"];
	[resImgDict setObject: @"oc_buu_nhoi_thit2.jpg" forKey:@"data"];
	[resImgDict setObject:@"Ốc Bưu Nhồi Thịt" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:18] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	resImgDict = [[NSMutableDictionary alloc] init];
	[resImgDict setObject:[NSNumber numberWithInt:14] forKey:@"id"];
	[resImgDict setObject: @"hutieutruongdinh.jpg" forKey:@"data"];
	[resImgDict setObject:@"Hủ Tiếu Trương Định" forKey:@"name"];
	[resImgDict setObject:[NSNumber numberWithInt:19] forKey:@"restaurantId"];
	[resImgArray addObject:resImgDict];
	[resImgDict release];
	
	for (int i = 0; i < [resImgArray count]; i++) {
		NSMutableDictionary *dict = [resImgArray objectAtIndex:i];
		RestaurantImage *restaurantImage = [NSEntityDescription insertNewObjectForEntityForName:@"RestaurantImage" inManagedObjectContext:managedObjectContext];
		restaurantImage.id = [dict objectForKey:@"id"];
        UIImage *thumbnail = [UIImage imageNamed:[dict objectForKey:@"data"]];
        NSData *thumbnailData =UIImagePNGRepresentation(thumbnail);
		restaurantImage.data = thumbnailData;
		restaurantImage.name = [dict objectForKey:@"name"];
		restaurantImage.restaurantId = [dict objectForKey:@"restaurantId"];
	}
	[managedObjectContext save:&err];
	return YES;
}

+ (NSArray *) getRestaurantImages{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"RestaurantImage" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}

+ (RestaurantImage *) getRestaurantImageWithId:(int)restaurantImageId{
	RestaurantImage* restaurantImage = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *restaurantImageArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", restaurantImageId];	
	@try {
		restaurantImageArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"RestaurantImage" :predicate :nil :NO :managedObjectContext];
		
		if ([restaurantImageArray count] != 0)
		{
			restaurantImage = [restaurantImageArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return restaurantImage;
}

+ (RestaurantImage *) getRestaurantImagesWithRestaurant:(int)restaurantId{
	RestaurantImage *restauranImage = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *restaurantImageArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"restaurantId = %d", restaurantId];	
	@try {
		restaurantImageArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"RestaurantImage" :predicate :nil :NO :managedObjectContext];
		if ([restaurantImageArray count] != 0)
		{
			restauranImage = [restaurantImageArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return restauranImage;
}

@end
